Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Random package not depend on global state #146

Merged
merged 7 commits into from
Mar 15, 2018
Merged

Conversation

nicolodavis
Copy link
Member

@nicolodavis nicolodavis commented Mar 13, 2018

We have the following problem at the moment:

The Random package is bundled into react.js, core.js and client.js. When the user calls the API, they use the version in core.js. However, when the framework tries to persist the PRNG state, it uses the version in react.js (if using a React client). This loses the "global" state, and makes the Random package always generate the same numbers, regardless of which seed is used.

Fix

This PR injects the Random API into ctx instead, so you would use it like:

moves: {
  rollDie: (G, ctx) => ({ ...G, die: ctx.random.D6() })
}

This also has the nice property that the individual move sub-reducers are now pure.

Checklist

  • Use a separate branch in your local repo (not master).
  • Test coverage is 100% (or you have a story for why it's ok).

@nicolodavis
Copy link
Member Author

@Stefan-Hanke @sladwig Can you take a look?

@coveralls
Copy link

coveralls commented Mar 13, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 143cfdc on fix-random into 43fbc42 on master.

@@ -84,11 +86,12 @@ export function createGameReducer({ game, numPlayers, multiplayer }) {
}

// Init PRNG state.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment is out of date.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

src/core/flow.js Outdated
@@ -278,7 +278,8 @@ export function FlowWithPhases({
const startTurn = function(state, config) {
const ctx = { ...state.ctx };
const G = config.onTurnBegin(state.G, ctx);
const _undo = [{ G, ctx }];
const { random, ...ctxWithoutAPI } = ctx; // eslint-disable-line no-unused-vars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to hide how random is attached to ctx. How about a function inside random.js that performs that action?

How does this scale to, say, the Events API? Does this have to be attached/stripped likewise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The Events API will probably have a similar attach / detach mechanism for ctx.

@Stefan-Hanke
Copy link
Contributor

Looks ok to me.

I had expected you'd need to change the JSON inside index.test.js also, but then, why should it change; the seed needs to be stored anyway...

@sladwig
Copy link
Contributor

sladwig commented Mar 14, 2018

Looks goot to me too, will test it later in game.

Copy link
Contributor

@sladwig sladwig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just checked in game and works like a charm :)

@nicolodavis nicolodavis merged commit 8969433 into master Mar 15, 2018
@nicolodavis nicolodavis deleted the fix-random branch March 15, 2018 06:04
sedobrengocce pushed a commit to sedobrengocce/boardgame.io that referenced this pull request Mar 21, 2018
* remove global state

* update docs

* remove from package

* update docs

* update docs

* review comments

* update docs/react/boardgameio.min.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants